"""添加用户页面"""
from page.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select


class addUserPage(BasePage):
    url = "/crm5/index.php?m=user&a=add"
    # 定位器
    # 用户名的定位器
    username_send_locator = (By.XPATH,
                             "/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[1]/td[2]/input")
    # 密码的定位器
    password_send_locator = (By.ID, "password")
    # 用户类别的定位器
    category_id_locator = (By.XPATH,
                           "/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[3]/td[2]/select")
    # 部门的定位器
    department2_locator = (By.ID, "department2")
    # 岗位的定位器
    role2_locator = (By.ID, "role2")
    # 保存按钮定位器
    btn_save_add_users_locator = (By.CSS_SELECTOR,
                                  "div.tab-pane.active input[name='submit'][value='添加']")
    # 邮箱邀请的定位器
    email_invited_locator = (By.LINK_TEXT, "邮箱邀请")
    # 用户名输入框
    email_username_send_locator = (By.XPATH,
                             "/html/body/div[5]/div[2]/div/div/div[1]/form/table/tbody/tr[1]/td[2]/input")
    # 邮箱输入框
    email_send_locator = (By.XPATH,
                          "/html/body/div[5]/div[2]/div/div/div[1]/form/table/tbody/tr[2]/td[2]/input")
    # 用户类型下拉框
    email_category_id_locator = (By.XPATH,
                           '/html/body/div[5]/div[2]/div/div/div[1]/form/table/tbody/tr[3]/td[2]/select')
    # 部门的定位器
    email_department_locator = (By.XPATH,
                          '/html/body/div[5]/div[2]/div/div/div[1]/form/table/tbody/tr[4]/td[2]/select')
    # 岗位的定位器
    role_locator = (By.ID, "role")
    # 添加按钮
    email_btn_save_add_users_locator = (By.CSS_SELECTOR,
                                  '#add > table > tbody > tr:nth-child(6) > td:nth-child(2) > input:nth-child(1)')
    #邮箱格式错误
    email_failed_locator = (By.CSS_SELECTOR,
                            'body > div.container > div.row > div > div.alert.alert-error')


    def input_username(self, username):
        """输入用户名"""
        self.driver.find_element(*self.username_send_locator).send_keys(username)
    def input_password(self, password):
        """输入密码"""
        self.driver.find_element(*self.password_send_locator).send_keys(password)
    def choose_category(self, category):
        """用户类别选择方法"""
        # 找到用户类别下拉框,选择第category个
        category_id_element = self.driver.find_element(*self.category_id_locator)
        category_obj = Select(category_id_element)
        category_obj.select_by_index(category)
    def choose_department(self, department):
        """部门选择方法"""
        department2_element = self.driver.find_element(*self.department2_locator)
        department2_obj = Select(department2_element)
        department2_obj.select_by_index(department)
    def choose_role(self, role):
        """岗位选择方法"""
        role_element = self.driver.find_element(*self.role2_locator)
        role_obj = Select(role_element)
        role_obj.select_by_index(role)
    def click_save(self):
        """点击保存"""
        self.driver.find_element(*self.btn_save_add_users_locator).click()
    def click_email_invited(self):
        #点击邮箱邀请
        self.driver.find_element(*self.email_invited_locator).click()
    def input_username_email(self, email_username):
        #输入用户名
        self.driver.find_element(*self.email_username_send_locator).send_keys(email_username)
    def input_email(self,email):
        #输入邮箱
        self.driver.find_element(*self.email_send_locator).send_keys(email)
    def choose_email_category(self, category=1):
        #找到用户类别下拉框,选择第一个
        self.category_id_element=self.driver.find_element(*self.email_category_id_locator)
        category_obj=Select(self.category_id_element)
        category_obj.select_by_index(category)
    def choose_email_department(self, department=1):
        #找到部门选择第一个
        department_element=self.driver.find_element(*self.email_department_locator)
        department_obj=Select(department_element)
        department_obj.select_by_index(department)
    def choose_email_role(self,role=1):
    #找到岗位选择第一个
        role_element=self.driver.find_element(*self.role_locator)
        role_obj=Select(role_element)
        role_obj.select_by_index(role)

    def click_email_save(self):
        # 点击保存
        self.driver.find_element(*self.email_btn_save_add_users_locator).click()
    def add_failed_text(self):
        return self.driver.find_element(*self.email_failed_locator).text
